package com.anp.business.task;

import com.anp.business.common.QuartzConfig;
import com.anp.business.service.IZhCertifyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author yicky zhang
 * @create 2018-09-10 下午2:15
 * @desc
 **/
@Component
public class Task implements SchedulingConfigurer {
    @Autowired
    private IZhCertifyService zhCertifyService;
    @Autowired
    private QuartzConfig quartzConfig;
    private static final Logger logger = LoggerFactory.getLogger(Task.class);

//    @Scheduled(cron =)
//    public void task() {
//        logger.info("定时任务开始");
//        zhCertifyService.doQuartz();
//    }


    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.addTriggerTask(doTask(), getTrigger());
    }

    private Runnable doTask() {
        return new Runnable() {
            @Override
            public void run() {
                logger.info("定时任务开始");
                zhCertifyService.doQuartz(quartzConfig.getLimit());
            }
        };
    }

    private Trigger getTrigger() {
        return new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                CronTrigger trigger = new CronTrigger(quartzConfig.getCron());
                return trigger.nextExecutionTime(triggerContext);
            }
        };
    }
}
